<?php
        require_once('auth.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML>
<HEAD>
<TITLE>Calculate RDCs</TITLE>
<link href="style.css" rel="stylesheet" type="text/css" />
</HEAD>
<script type="text/javascript">
function newPopup(url) {
        popupWindow = window.open(
        url, 'popUpWindow', 'height=450,width=800,left=50,top=50,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=yes')
}
function newPopup2(url) {
        popupWindow = window.open(
        url,'popUpWindow','height=450,width=400,left=50,top=50,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=yes')
}
</script>

<BODY>
<center>
<img src="redcat_logo.jpg">
</center>
<div>
<a href="JavaScript:newPopup('account.php');" style="float:left">My Files</a>
<a href="logout.php" style="float:right">Logout</a>
</div><br />

<div>
<a href="JavaScript:newPopup2('help-menu.php');" style="float:left">Click here for help</a>
</div><br /><br />

<div>
  <form method="GET" action="/REDCAT/use-file.php"><input type="submit" name="use" id="use" value="Use This Data" style="float:left"></form>
  <form method="GET" action="JavaScript:newPopup2('download.php');"><input type="submit" name="download" id="download" value="Download New Data" style="float:left"</form>
  <form method="GET" action="/REDCAT/return-upload.php"><input type="submit" name="return" id="return" value="Return to Data" style="float:right"></form>
</div><br /><br />

<H3 align="left">Enter data parameters:</H3>
<H3 align="center">
<table border="1">
  <form action="/REDCAT/calculated.php" method="POST" enctype="multipart/form-data">
<tr><td>Sxx:  <input type="text" name="sxx" id="sxx"></td>
<td>Syy:  <input type="text" name="syy" id="sxx"></td>
<td>Szz:  <input type="text" name="szz" id="szz"></td></tr>
<tr><td>Alpha:  <input type="text" name="a" id="a"></td>
<td>Beta:  <input type="text" name="b" id="b"></td>
<td>Gamma:  <input type="text" name="c" id="c"></td></tr>
<tr><td></td><td>Error:  <input type="text" name="error" id="error"></td><td></td></tr>
<tr><td></td><td align="center"><input type="submit" name="Calculate" value="Calculate" /></form></td><td></td></tr>
</table>
</H3>

<style type="text/css">
tr.filelisting:hover { background-color:#FFC }
</style>

<?php
//Grab user input
$sxx = $_POST["sxx"];
$syy = $_POST["syy"];
$szz = $_POST["szz"];

if( ($sxx + $syy + $szz) != 0 ) {
	echo '<script>alert("Sxx, Syy, and Szz must add to 0!")</script>';
	echo '<script type="text/javascript">window.location = "rdc.php";</script>';
}
$a = $_POST["a"];
$b = $_POST["b"];
$c = $_POST["c"];
$error = $_POST["error"];
if ($sxx == '' || $syy == '' || $szz == '' || $a == '' || $b == '' || $c == '' || $error == '') {
	echo '<script>alert("One or more fields are missing!  Please fill in all required fields.")</script>';
	echo '<script type="text/javascript">window.location = "rdc.php";</script>';
}
$_SESSION['SESS_ERR'] = $error;
$file = $_SESSION['SESS_CURR_FILE'];
$login = $_SESSION['SESS_LOGIN'];
$currentdir = getcwd();
$target = $currentdir."/workspace/".$login;
$seed = mt_rand();

//Run command line to calculate RDC values
$rdc = `RDC $file $sxx $syy $szz $a $b $c $error $seed > $target/rdc.txt`;
$data = fopen($target."/rdc.txt", "r");
$file = file($file);
//Display data with new RDC values and error replacing old values
echo '<br><br><H3 align="center">';
echo "Sxx:  $sxx  Syy:  $syy  Szz:  $szz  Alpha:  $a  Beta:  $b  Gamma:  $c  Error:  $error";
echo '</H3>';
echo '<H5 align = "center">';
echo '<table border="1">';
echo '<tr><th align = "center">Eq.#</th><th align = "center">X1</th><th align = "center">Y1</th><th align = "center">Z1</th><th align = "center">X2</th><th align = "center">Y2</th><th align = "center">Z2</th><th align = "center">Dipol</th><th align = "center">Error</th><th align = "center">Comments</th></tr>';
$eq = 1;
foreach($file as $line)
{
	$l1 = fgets($data);
	$calc = preg_split("/[\s]+/", $l1);
        $comment = preg_split('(\/\*)', $line);
        $value = preg_split("/[\s]+/", $comment[0]);
        echo "<tr class = \"filelisting\"><td>$eq</td><td>$value[0]</td><td>$value[1]</td><td>$value[2]</td><td>$value[3]</td><td>$value[4]</td><td>$value[5]</td><td>$calc[0]</td><td>$error</td><td>/*$comment[1]</td></tr>";
        $eq++;
}
echo '</table>';
echo '</H3>';
fclose($data);

session_write_close();
?>

</HTML>
